#import "SMUserFavoritesViewController.h"
#import "SMListCardCell.h"
#import <MJRefresh/MJRefresh.h>

@interface SMUserFavoritesViewController ()
@property(nonatomic, copy) NSString *uid;
@property(nonatomic, strong) NSMutableArray<NSDictionary*> *data;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, assign) BOOL hasMore;
@end

@implementation SMUserFavoritesViewController
- (instancetype)initWithUserId:(NSString *)uid { if (self=[super initWithStyle:UITableViewStylePlain]){ _uid=[uid copy]; } return self; }
- (void)viewDidLoad { [super viewDidLoad]; self.tableView.backgroundColor=UIColor.systemGroupedBackgroundColor; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.tableView registerClass:SMListCardCell.class forCellReuseIdentifier:@"cell"]; self.data=[NSMutableArray array]; self.page=1; self.hasMore=YES; self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)]; self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)]; [self.tableView.mj_header beginRefreshing]; }
- (void)refresh { self.page=1; self.hasMore=YES; [self request:self.page]; }
- (void)loadMore { if(self.hasMore){ self.page++; [self request:self.page]; } else [self.tableView.mj_footer endRefreshingWithNoMoreData]; }
- (void)request:(NSInteger)page { NSMutableArray *arr=[NSMutableArray array]; for(int i=0;i<10;i++){ NSString *img=[NSString stringWithFormat:@"https://picsum.photos/seed/fav_%@_%ld_%d/520/360", self.uid,(long)page,i]; NSString *title=[NSString stringWithFormat:@"Ta收藏的内容 #%d", i+1]; NSString *sub=@"含量高、杂质少、支持上门验货，长期合作～"; NSString *meta=@"苏州 · 最近"; [arr addObject:@{@"img":img,@"title":title,@"sub":sub,@"meta":meta}]; } if(page==1)[self.data removeAllObjects]; [self.data addObjectsFromArray:arr]; self.hasMore=arr.count>=10; [self.tableView reloadData]; [self.tableView.mj_header endRefreshing]; if(self.hasMore)[self.tableView.mj_footer endRefreshing]; else [self.tableView.mj_footer endRefreshingWithNoMoreData]; }
- (NSInteger)tableView:(UITableView*)t numberOfRowsInSection:(NSInteger)s{ return self.data.count; }
- (CGFloat)tableView:(UITableView*)t heightForRowAtIndexPath:(NSIndexPath*)i{ return 110; }
- (UITableViewCell*)tableView:(UITableView*)t cellForRowAtIndexPath:(NSIndexPath*)i{ SMListCardCell *c=[t dequeueReusableCellWithIdentifier:@"cell" forIndexPath:i]; NSDictionary *d=self.data[i.row]; [c configureWithImageURL:d[@"img"] title:d[@"title"] subtitle:d[@"sub"] meta:d[@"meta"]]; return c; }
@end
